Skip to contentMethod: mergeValue(FieldSpecification, Object, Object, Object, Descriptor)
1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.merge;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.CollectionType;
19: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
20: import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute;
21: import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification;
22: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
23: import cz.cvut.kbss.jopa.utils.CollectionFactory;
24: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
25: import cz.cvut.kbss.jopa.utils.MetamodelUtils;
26:
27: import java.util.Collection;
28:
29: class CollectionValueMerger implements ValueMerger {
30:
31: private final UnitOfWorkImpl uow;
32: private final ManagedTypeValueMerger managedTypeMerger;
33:
34: CollectionValueMerger(UnitOfWorkImpl uow, ManagedTypeValueMerger managedTypeMerger) {
35: this.uow = uow;
36: this.managedTypeMerger = managedTypeMerger;
37: }
38:
39: @Override
40: public void mergeValue(FieldSpecification<?, ?> att, Object target, Object originalValue, Object mergedValue,
41: Descriptor attributeDescriptor) {
42: final Collection<?> mergedCol = (Collection<?>) mergedValue;
43:• if (mergedCol == null) {
44: EntityPropertiesUtils.setFieldValue(att.getJavaField(), target, null);
45: return;
46: }
47:
48: final Collection<Object> newValue = CollectionFactory
49: .createDefaultCollection(CollectionType.fromClass(att.getJavaType()));
50: boolean elemTypeManaged = false;
51:• if (att instanceof PluralAttribute) {
52: elemTypeManaged = uow.isEntityType(((PluralAttribute) att).getBindableJavaType());
53: }
54:• for (Object item : mergedCol) {
55:• newValue.add(elemTypeManaged ? managedTypeMerger.getValueToSet(item, attributeDescriptor) : item);
56: }
57: extendModuleExtractionSignature(att, newValue);
58: EntityPropertiesUtils.setFieldValue(att.getJavaField(), target, newValue);
59: }
60:
61: private void extendModuleExtractionSignature(FieldSpecification<?, ?> att, Collection<?> value) {
62: if (att instanceof TypesSpecification) {
63: MetamodelUtils.checkForModuleSignatureExtension(value, uow.getMetamodel());
64: }
65: }
66: }